<template>
  <div class="main-box">
    <!--头部-->
    <header>
      <!--logo和搜索-->
      <el-row>
        <el-col :span="24">
          <el-row justify="center">
            <el-col :span="18">
              <el-row align="middle">
                <!--logo-->
                <el-col :span="6">
                  <img src="../assets/car-logo.png">
                </el-col>
                <!--搜索-->
                <el-col :span="18">
                  <el-row justify="end" :gutter="10" align="middle">
                    <!--搜索框-->
                    <el-col :span="12">
                      <div class="demo-input-size">
                        <el-input
                          v-model="searchValue"
                          class="w-50 m-2"
                          size="large"
                          placeholder="Please enter the search content"
                        />
                      </div>
                    </el-col>
                    <!--搜索按钮-->
                    <el-col :span="3">
                      <el-button @click="search">
                        <el-icon style="vertical-align: middle">
                          <Search />
                        </el-icon>
                        <span style="vertical-align: middle"> Search </span>
                      </el-button>
                    </el-col>
                  </el-row>
                  
                  
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    <!--轮播图-->
    <el-row justify='center'>
      <el-col :span="24">
          <el-carousel :interval="5000" arrow="always" height="420px">
            <el-carousel-item v-for="item in carouselArr" :key="item">
              <img :src="item.img" class="carousel1">
            </el-carousel-item>
          </el-carousel>
      </el-col>
    </el-row>
    </header>
    <!--主要内容-->
    <div>
      <el-row justify="center">
        <el-col :span="18">
          <el-row :gutter="20">
            <el-col :span="6" v-for='(item,index) in showData' :key='index'>
              <div class="contentc" @click="moreCar(index)">
                <img :src="item.path">
                <h3>{{ item.name }}</h3>
              </div>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
    <!--脚部-->
    <Footer/>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import audiArr from '../data/data1'
import axios from 'axios'
import Footer from '../views/Footer.vue' 
import { ElMessage } from 'element-plus'
export default {
  components:{
    Footer
  },
  name: '',
  setup() {
    const router = useRouter()
    //轮播图片
    const carouselArr=ref([
      {img:require('../assets/car1.png')},
      {img:require('../assets/car2.png')},
      {img:require('../assets/car3.png')},
    ])
    //搜索框
    const searchValue = ref()

    //搜索按钮
    const search = ()=>{
      if(searchValue.value==undefined){
        ElMessage({
          showClose: true,
          message: '请输入',
          center: true,
        })
      }else{
        axios.get('http://localhost:3000/data1?q='+searchValue.value+'&details?q='+searchValue.value).then(res=>{
          if(res.data.length==0){
            ElMessage({
              showClose: true,
              message: '抱歉，暂无相关内容',
              center: true,
            })
          }else{
            showData.value = res.data
          }
        })
      }
      
    }
    //点击查看更多 跳转页面
    const moreCar = (index)=>{
      sessionStorage.setItem('data',JSON.stringify(showData.value[index]))
      router.push('/morecar')
    }
    //请求数据
    const showData = ref([])
    const getData = ()=>{
      axios.get('http://localhost:3000/data1').then(res=>{
        showData.value = res.data
      }).catch(err=>{
        console.log(err);
      })
    }
    onMounted(() => {
     getData()
    })
    return {
      carouselArr,searchValue,audiArr,moreCar,showData,search
    }
  },
}

</script>
<style scoped lang='scss'>
    .main-box{
      background-color: #F2F3F5;
      .carousel1{
        width: 100%;
        height: 422px;
      }
      .contentc{
        margin: 10px;
        padding: 10px;
        border: 1px #afafaf solid;
        text-align: center;
        cursor: pointer;
        img{
          width: 100%;
        }
      }
      .contentc:hover{
        opacity: 0.5;
      }
    }
</style>
